package h0903;

/**
 * @author 不知名帅哥
 * @version 1.0
 * @description 长度最小的子数组
 * @date 2024/9/3 22:01
 */
public class MinLenSubArray {
    public static void main(String[] args) {
        int[] nums = {2, 3, 1, 2, 4, 3};
        int target = 7;
        System.out.println(minSubArrayLen(target, nums));
    }

    public static int minSubArrayLen(int target, int[] nums) {
        int start = 0;
        int sum = 0;
        int minLen = Integer.MAX_VALUE;
        for (int end = 0; end < nums.length; end++) {
            sum += nums[end];
            while (sum >= target) {
                minLen = Math.min(minLen, end - start+1);
                sum -= nums[start];
                start++;
            }
        }
        return minLen;
    }
}
